package com.wandian.view.ui
{
	import com.wandian.util.GDI;
	import com.wandian.util.Res;
	
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class SimpleButton  extends Sprite
	{
		private var key:String;
		private var clickHandler:Function
		private var enable:Boolean=true;
		
		public function SimpleButton(key:String)
		{
			super();
			setKey(key);
			this.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onUp);
			this.addEventListener(MouseEvent.CLICK, onClick);
			
		}
		
		public function setKey(key:String):void
		{
			this.key=key
			onUp();
		}
		
		public function setClickHandler(handler:Function):void
		{
			this.clickHandler=handler
		}
		
		private function onOver(e:MouseEvent):void
		{
			if (!enable)
			{
				return
			}
			var bmd:BitmapData=Res.get(key + "_btn_over")
			if (bmd == null)
			{
				return
			}
			var g:Graphics=this.graphics
			g.clear();
			
			GDI.drawBitmapData(g, 0, 0, bmd);
		}
		
		private function onUp(e:MouseEvent=null):void
		{
			if (!enable)
			{
				return
			}
			var g:Graphics=this.graphics
			g.clear();
			GDI.drawBitmapData(g, 0, 0, Res.get(key + "_btn"));
			
		}
		
		private function onDisable():void
		{
			
			var bmd:BitmapData=Res.get(key + "_btn_disable")
			if (bmd == null)
			{
				return
			}
			var g:Graphics=this.graphics
			g.clear();
			GDI.drawBitmapData(g, 0, 0, bmd);
			
		}
		
		private function onClick(e:MouseEvent):void
		{
			if (!enable)
			{
				return
			}
			if (clickHandler != null)
			{
				this.clickHandler();
			}
		}
		
		public function setEnable(f:Boolean):void
		{
			enable=f
			if (enable)
			{
				onUp();
			}
			else
			{
				onDisable();
			}
		}
		
	}
}
